package com.imooc.firstappdemo.repository;

import com.imooc.firstappdemo.domain.User;
import org.springframework.stereotype.Repository;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * {@link User}{@link Repository}
 * @author zzc
 * @date 2018/3/26 下午11:29
 */
@Repository
public class UserRepository {

    /**
     * 采用内存型的存储方式 -> Map
     */
    private ConcurrentMap<Integer,User> repository=new ConcurrentHashMap <>();
    private final static  AtomicInteger idGenerator = new AtomicInteger();

    /**
     * 保存用户对象
     * @param user
     * @return 如果保存成功，返回<code>true</code> 否则返回<code>false</code>
     */
    public boolean save(User user){
        boolean success = false;
        // ID 从 1 开始
        Integer id = idGenerator.incrementAndGet();
        user.setId(id);
        return repository.put(id,user)==null;
    }

}























